package com.example.quanxin.controller;/*
 *
 *   welcome:11311
 *   创建时间:2023/5/20
 *
 */

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.quanxin.config.GlobalConfig;
import com.example.quanxin.entity.*;
import com.example.quanxin.service.ApiService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.annotation.Resource;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/")

//@CrossOrigin(value = "/")
public class ApiController {

    @Resource
    private ApiService apiService;



    @GetMapping("/getAllCouse")
    @Operation(summary ="获取所有课程")
    public ReturnT getAllCourse(){
        Map<String, Long> courseMapping = GlobalConfig.getCourseMapping();
        return ReturnT.SUCCESS(courseMapping);
    }

    @GetMapping("/getAllClass")
    @Operation(summary ="获取所有班级")
    public ReturnT getAllClass(){
        Map<String, Long> classMapping = GlobalConfig.getClassMapping();
        return ReturnT.SUCCESS(classMapping);
    }

    @PostMapping("/login")
    public ReturnT login(@RequestBody LoginParam loginParam) throws IOException {
        apiService.getAndStoreCookies(loginParam);
        return ReturnT.SUCCESS();
    }

    @PostMapping("/getActiveList")
    @Operation(summary ="获取所有正在进行的的活动")
    public ReturnT getActiveList(@RequestBody ActiveParam activeParam){
        JSONArray active = apiService.getActive(activeParam);
        return ReturnT.SUCCESS(active);
    }

    @PostMapping("/getAllActiveList")
    @Operation(summary ="获取所有所有活动")
    public ReturnT getAllActiveList(@RequestBody ActiveParam activeParam){
        activeParam.setStatus(0L);
        JSONArray result = apiService.getActive(activeParam);
        activeParam.setStatus(1L);
        result.addAll(apiService.getActive(activeParam));
        activeParam.setStatus(2L);
        result.addAll(apiService.getActive(activeParam));
        return ReturnT.SUCCESS(result);
    }

    @GetMapping("/getAllScore")
    public ReturnT getAllScore(Long activeId, Long classId) throws Exception {
        List<Score> allScore = apiService.getAllScore(activeId, classId);
        if(! CollectionUtils.isEmpty(allScore)){
            allScore = allScore.stream().sorted(new Comparator<Score>() {
                @Override
                public int compare(Score o1, Score o2) {
                    return Double.compare(o2.getScore(), o1.getScore());
                }
            }).collect(Collectors.toList());
            int maxIndex = Integer.min(allScore.size(), 15);
            allScore = allScore.subList(0, maxIndex);
        }
        return ReturnT.SUCCESS(allScore);
    }

    @PostMapping("/getHistoryAllScore")
    @Operation(summary ="获取学生历史成绩")
    public ReturnT<List<Score>> getHistoryAllScore(@RequestBody HistoryScore historyScore) throws Exception {
        return ReturnT.ok(apiService.getHistoryScore(historyScore));
    }

    @PostMapping("/getGroupLiveness")
    @Operation(summary ="小组活跃度")
    public ReturnT<List<GroupLiveness>> getGroupLiveness(@RequestBody HistoryScore historyScore) throws Exception {
        return ReturnT.ok(apiService.getGroupLiveness(historyScore));
    }

    @PostMapping("/getStudentLiveness")
    @Operation(summary ="学生活跃度")
    public ReturnT getStudentLiveness(@RequestBody StudentLivenessReq req) throws Exception {
        return ReturnT.SUCCESS(apiService.getStudentLiveness(req));
    }
    @PostMapping("/getGroupLivenessRatio")
    @Operation(summary ="小组活跃度增减")
    public ReturnT getGroupLivenessRatio(@RequestBody HistoryScore historyScore) throws Exception {
        return ReturnT.SUCCESS(apiService.getGroupLivenessRatio(historyScore));
    }

    @GetMapping("/getActCount")
    @Operation(summary ="活动数据汇总")
    public ReturnT<ActCount> getActCount(@RequestParam("courseId")  @Parameter(description="课程id")Long courseId,
                               @RequestParam("classId") @Parameter(description="班级id") Long classId) throws Exception {
        ActiveParam activeParam =new ActiveParam();
        activeParam.setCourseId(courseId);
        activeParam.setJclassId(classId);
        JSONArray result = apiService.getActive(activeParam);
        activeParam.setStatus(0L);
        result.addAll(apiService.getActive(activeParam));
        activeParam.setStatus(2L);
        result.addAll(apiService.getActive(activeParam));
        ActCount actCount=new ActCount();
        if (result!=null){
           actCount.setActNum(result.size());
            HistoryScore historyScore=new HistoryScore();
            List<Long> activeIdList=new ArrayList<>();
            for(int i = 0; i < result.size(); i ++){
                JSONObject tmp = result.getJSONObject(i);
                Long id = tmp.getLong("id");
                activeIdList.add(id);
            }
            historyScore.setActiveIdList(activeIdList);
            historyScore.setClassId(classId);
            List<Score> historyScore1 = apiService.getHistoryScore(historyScore);
            actCount.setStudentNum(historyScore1.size());
            List<GroupScore> groupScore = apiService.getGroupScore(historyScore);
            double averageScore = 0;
            for (GroupScore gs : groupScore) {
                averageScore += gs.getScore();
            }
            averageScore /= groupScore.size();
            actCount.setGroupAvg(averageScore);
        }
        else{
            actCount.setActNum(0);
            actCount.setStudentNum(0);
            actCount.setGroupAvg(0.0);
        }
        return    ReturnT.ok(actCount);
    }



    @PostMapping("/getGroupScore")
    @Operation(summary ="小组得分")
    public ReturnT getGroupScore(@RequestBody HistoryScore historyScore) throws Exception{
        return ReturnT.SUCCESS(apiService.getGroupScore(historyScore));
    }

    @GetMapping("/addClass")
    public ReturnT addClass(String className, Long id){
        if(className != null && id != null){
            GlobalConfig.getClassMapping().put(className, id);
        }
        return ReturnT.SUCCESS();
    }

    @GetMapping("/addCourse")
    public ReturnT addCourse(String courseName, Long id){
        if(courseName != null && id != null){
            GlobalConfig.getCourseMapping().put(courseName, id);
        }
        return ReturnT.SUCCESS();
    }

    @GetMapping("/removeClass")
    public ReturnT removeClass(String className){
        if(className != null){
            GlobalConfig.getClassMapping().remove(className);
        }
        return ReturnT.SUCCESS();
    }

    @GetMapping("/removeCourse")
    public ReturnT removeCourse(String courseName){
        if(courseName != null){
            GlobalConfig.getCourseMapping().remove(courseName);
        }
        return ReturnT.SUCCESS();
    }



}
